// File Name: rs485_interface.c
// Author: kukukukuku~
// Date: 2024-12-20

#include "string.h"
#include "./RS485/inc/rs485_interface.h"

/**
 * @brief   RS485对象初始化
 * @param   rs485Obj:   RS485对象
 * @param   txBuffer:   RS485发送缓冲区
 * @param   rxBuffer:   RS485接收缓冲区
 * @retval  None
 * @note    None
*/
void rs485_object_init(RS485_OBJECT_T * const rs485Obj, const uint8_t * const txBuffer, const uint8_t * const rxBuffer)
{
    if(rs485Obj == NULL || txBuffer == NULL || rxBuffer == NULL || txBuffer == rxBuffer)
        return;
    
    rs485Obj->pTxBuffer = (uint8_t *)txBuffer;
    rs485Obj->pRxBuffer = (uint8_t *)rxBuffer;
    rs485Obj->rxFinishFlag = RS485_RX_STATE_UNCOMPLETE;
    rs485Obj->rxIndex = 0;
}


/**
 * @brief   RS485空闲中断回调函数
 * @param   rs485Obj:   RS485对象
 * @retval  None
 * @note    None
*/
void rs485_int_idle_callback(RS485_OBJECT_T * const rs485Obj)
{
    if(rs485Obj == NULL)
        return;
    
    rs485Obj->rxFinishFlag = RS485_RX_STATE_COMPLETE;
}


/**
 * @brief   RS485接收中断回调函数
 * @param   rs485Obj:   RS485对象
 * @retval  None
 * @note    None
*/
void rs485_int_rx_callback(RS485_OBJECT_T * const rs485Obj)
{
    if(rs485Obj == NULL || rs485Obj->pRxBuffer == NULL)
        return;
    
    if(rs485Obj->rxFinishFlag == RS485_RX_STATE_UNCOMPLETE && rs485Obj->rxIndex < RS485_RX_BUFFER_LEN)
        rs485Obj->pRxBuffer[rs485Obj->rxIndex++] = rs485_usart_receive_data();
}


/**
 * @brief   RS485清除接收完成标志
 * @param   rs485Obj:   RS485对象
 * @retval  None
 * @note    None
*/
void rs485_rx_finish_flag_clear(RS485_OBJECT_T * const rs485Obj)
{
    if(rs485Obj == NULL)
        return;
    
    rs485Obj->rxFinishFlag = RS485_RX_STATE_UNCOMPLETE;
}


/**
 * @brief   RS485获取接收完成标志
 * @param   rs485Obj:   RS485对象
 * @retval  接收完成标志
 *          @arg RS485_RX_STATE_UNCOMPLETE: 接收未完成
 *          @arg RS485_RX_STATE_COMPLETE:   接收已完成
 * @note    None
*/
RS485_RX_STATE_T rs485_rx_finish_flag_get(const RS485_OBJECT_T * const rs485Obj)
{
    if(rs485Obj == NULL)
        return RS485_RX_STATE_UNCOMPLETE;
    
    return rs485Obj->rxFinishFlag;
}


/**
 * @brief   RS485清除接收数据长度
 * @param   rs485Obj:   RS485对象
 * @retval  None
 * @note    None
*/
void rs485_rx_data_len_clear(RS485_OBJECT_T * const rs485Obj)
{
    if(rs485Obj == NULL)
        return;
    
    rs485Obj->rxIndex = 0;
}


/**
 * @brief   RS485获取接收数据长度
 * @param   rs485Obj:   RS485对象
 * @retval  接收数据长度
 * @note    None
*/
uint16_t rs485_rx_data_len_get(const RS485_OBJECT_T * const rs485Obj)
{
    return rs485Obj == NULL ? 0 : rs485Obj->rxIndex;
}


/**
 * @brief   RS485接收数据处理
 * @param   rs485Obj:   RS485对象
 * @retval  None
 * @note    None
*/
void rs485_received_data_handle(RS485_OBJECT_T * const rs485Obj)
{
    if(rs485Obj == NULL || rs485Obj->pRxBuffer == NULL)
        return;
    
    //数据处理
}


/**
 * @brief   RS485获取接收缓冲区
 * @param   rs485Obj:   RS485对象
 * @retval  接收缓冲区
 * @note    None
*/
uint8_t * rs485_rx_buffer_get(const RS485_OBJECT_T * const rs485Obj)
{
    return rs485Obj == NULL ? NULL : rs485Obj->pRxBuffer;
}


/**
 * @brief   RS485清空接收缓冲区
 * @param   rs485Obj:   RS485对象
 * @retval  None
 * @note    None
*/
void rs485_rx_buffer_clear(RS485_OBJECT_T * const rs485Obj)
{
    if(rs485Obj == NULL || rs485Obj->pRxBuffer == NULL)
        return;
    
    memset(rs485Obj->pRxBuffer, 0, RS485_RX_BUFFER_LEN);
}


/**
 * @brief   RS485发送数据
 * @param   rs485Obj:   RS485对象
 * @param   data:       要发送的数据
 * @param   dataLen:    要发送的数据长度
 * @retval  None
 * @note    None
*/
void rs485_send_data(RS485_OBJECT_T * const rs485Obj, const uint8_t * const data, const uint16_t dataLen)
{
    if(rs485Obj == NULL || rs485Obj->pTxBuffer == NULL || data == NULL || dataLen == 0 || dataLen > RS485_TX_BUFFER_LEN)
        return;
    
    memset(rs485Obj->pTxBuffer, 0, RS485_TX_BUFFER_LEN);
    memcpy(rs485Obj->pTxBuffer, data, dataLen);
    
    rs485_set_mode(RS485_MODE_SEND);
    rs485_usart_send_data(rs485Obj->pTxBuffer, dataLen);
    rs485_set_mode(RS485_MODE_RECEIVE);
}
